import SwiftUI
import UIKit // 导入UIKit便于使用TCP相关功能

// 传感器数据视图
struct SensorDataView: View {
    @EnvironmentObject var tcpCarService: TCPCarService
    
    var body: some View {
        VStack(spacing: 25) {
            Text("传感器数据")
                .font(.title)
                .fontWeight(.bold)
            
            // 数据卡片列表
            VStack(spacing: 15) {
                // 温度卡片
                SensorCard(
                    icon: "thermometer",
                    title: "温度",
                    value: String(format: "%.1f°C", tcpCarService.temperature),
                    color: .orange
                )
                
                // 湿度卡片
                SensorCard(
                    icon: "humidity.fill",
                    title: "湿度",
                    value: String(format: "%.1f%%", tcpCarService.humidity),
                    color: .blue
                )
                
                // 运行速度卡片
                SensorCard(
                    icon: "speedometer",
                    title: "运行速度",
                    value: "\(tcpCarService.speed)%",
                    color: .purple
                )
            }
            
            Button(action: {
                // 手动刷新传感器数据
                tcpCarService.requestSensorData()
            }) {
                Text("刷新数据")
                    .font(.headline)
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }
            .padding(.top, 20)
            
            Spacer()
        }
        .padding()
    }
}

// 预览
struct SensorDataView_Previews: PreviewProvider {
    static var previews: some View {
        SensorDataView()
            .environmentObject(TCPCarService())
    }
} 